package com.wt.struggle.goodbye.user.activity

import android.content.Intent
import android.os.Bundle
import android.os.Message
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import com.google.gson.reflect.TypeToken
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.Config
import com.wt.struggle.goodbye.base.ItemClickListener
import com.wt.struggle.goodbye.base.ProActivity
import com.wt.struggle.goodbye.base.Send
import com.wt.struggle.goodbye.info.NetInfo
import com.wt.struggle.goodbye.info.ProUserInfo
import com.wt.struggle.goodbye.sq.activity.PickDetailsActivity
import com.wt.struggle.goodbye.user.adapter.FansAdapter
import kotlinx.android.synthetic.main.fans_layout.*
import kotlinx.android.synthetic.main.head.*

class FansActivity : ProActivity() {

    override fun handler(what: Int, arg1: Int, netInfo: NetInfo) {

        when (what) {
            Send.GET_USER_INFO -> {

                removeLoadDialog()
                if (netInfo.code == Config.SUCCESS) {
                    val result = netInfo.dataResult
                    if (result != null && result != "" && result != "null") {
                        val arr: ArrayList<ProUserInfo> =
                            gson!!.fromJson(result, object : TypeToken<ArrayList<ProUserInfo>>() {}.type)
                        adapter.updateData(arr)
                    }
                }
            }

            Send.ATTENTION_CODE -> {

                if (netInfo.code == Config.SUCCESS) {
                    list.removeAt(indexPosition)
                    adapter.notifyItemRemoved(indexPosition)
                    showToastShort("操作成功")
                }
            }
        }

    }

    var userInfo: ProUserInfo? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.fans_layout)
        tvTitle.text = "粉丝"

        userInfo = intent.getParcelableExtra("userInfo")

        imageBack.setOnClickListener {
            finish()
        }

        initAdapter()

        getFansList()
    }

    var page: Int = 1

    private fun getFansList() {
        if (userInfo != null) {
            send!!.getMyFansList(userInfo!!.appUserId!!, page, handler)

        } else {
            send!!.getMyFansList("", page, handler)
        }

        showLoadDialog("获取中")

    }


    lateinit var adapter: FansAdapter
    var list = arrayListOf<ProUserInfo>()

    private fun initAdapter() {
        fansRecyclerView.layoutManager = LinearLayoutManager(this)
        fansRecyclerView.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
        adapter = FansAdapter(this, list)
        fansRecyclerView.adapter = adapter
        adapter.itemClickListener = object : ItemClickListener {
            override fun onItemClick(position: Int) {

                val info = list[position]
                val intent = Intent(this@FansActivity, UserInfoActivity::class.java)
                intent.putExtra("user", info)
                startActivity(intent)
            }

            override fun onLongClick(position: Int) {

            }

        }

    }

    var indexPosition: Int = 0

    fun collect(position: Int) {
        this.indexPosition = position
        val info = list[position]
        send!!.attentionUser(info, handler)

    }
}